from datetime import datetime
from flask import Blueprint,request,jsonify
from flask import current_app as app
from db import db
from xtree import xtree
from utile.comm import get_data
bp_blue = Blueprint('comany',__name__)

@bp_blue.route('/test')
def test():
     str = '请求ip'+request.remote_addr + ',请求的url'+request.url+'错误信息'+'\n'
     
     app.logger.error(str)
     return  'Hello!'


@bp_blue.route('/cateM', methods=['POST', 'GET'])
def cateM():
    if request.method == 'POST':
        # 添加
        # 获取数据
        data = get_data()
        print(data)
        # 构造sql
        sql = "insert into organization values(0,'%s',%d,%s)" % (data['name'], int(data['pid']))
        print(sql)
        # 调用db1
        db.update(sql)
        # 返回结果
        return jsonify({"code": 200})
    else:
        # 无限级分类展示
        sql = "select * from organization"
        res = db.finds(sql)
        data = xtree(res)
     #    print(data)
        return jsonify({"code": 200, 'list': data})

